package day10.shop;

import java.io.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class ShopTest {
    public static void main(String[] args) {
        File file = new File("shop.txt");
        Scanner in = new Scanner(System.in);
        Shop shop =Shop.getFromFile();
        if(shop==null){
            //没找到，整一个新的
            shop=new Shop();
            System.out.println("输入新超市的名字：");
            shop.setName(in.next());
            System.out.println("输入新超市的地址：");
            shop.setAdr(in.next());
        }
        //存不存！！！???


        while (true) {
            System.out.println("1。添加新会员  2。用户涨分  3。兑换礼品 4。查询剩余积分 5.修改密码 6.显示所有会员信息 0退出");
            int n = in.nextInt();
            switch (n) {
                case 1:
                    Card c = new Card();
                    System.out.println("请输入新会员的卡号");
                    c.setNumber(in.nextInt());
                    if (shop.checkNumber(c.getNumber())) {
                        System.out.println("账号已经存在");
                        break;
                    }
                    System.out.println("请设置密码");
                    c.setPassword(in.next());
                    System.out.println("请输入新会员的姓名");
                    c.setName(in.next());
                    System.out.println("请输入新会员的手机号");
                    c.setPhone(in.next());
                    System.out.println("请输入新会员的积分");
                    c.setFen(in.nextDouble());
                    if (shop.add(c)) {
                        System.out.println("添加成功");
                    }
                    //保存一下，可以！
                    //分开保存，超级牛逼才可以
                    break;
                case 2:
                    System.out.println("请输入卡号");
                    int number = in.nextInt();
                    System.out.println("请输入密码");
                    String password = in.next();
                    if (!shop.checkPassword(number, password)) {
                        break;
                    }
                    System.out.println("请输入涨多少分");
                    double fen = in.nextDouble();
                    if (shop.addfen(number, fen)) {
                        System.out.println("涨分成功");
                    } else {
                        System.out.println("涨分失败");
                    }
                    break;
                case 3:
                    System.out.println("1：100分兑换餐巾纸一包；" +
                            "2：400分兑换剃须刀一把；" +
                            "3：50000分兑换汽车一个；");
                    System.out.println("请输入卡号");
                    number = in.nextInt();
                    System.out.println("请输入密码");
                    password = in.next();
                    System.out.println("请输入兑换的商品");
                    String shangpin = in.next();
                    if (shangpin.equals("1")) {
                        shangpin = "餐巾纸";
                    } else if (shangpin.equals("2")) {
                        shangpin = "剃须刀";
                    } else if (shangpin.equals("3")) {
                        shangpin = "汽车";
                    }
                    if (shop.diuhuan(number, password, shangpin)) {
                        System.out.println("恭喜你兑换" + shangpin + "成功");
                    } else System.out.println("兑换失败");
                    break;
                case 4:
                    System.out.println("查询剩下积分，请输入卡号");
                    number = in.nextInt();
                    System.out.println("请输入密码");
                    password = in.next();
                    fen = shop.findFen(number, password);
                    if (fen > 0) {
                        System.out.println("卡号" + number + "现在的积分为：" + fen);
                    }
                    break;
                case 5:
                    System.out.println("请输入卡号");
                    number = in.nextInt();
                    System.out.println("请输入现在的密码");
                    password = in.next();
                    if (!shop.checkPassword(number, password)) {
                        break;
                    }
                    System.out.println("请输入新密码");
                    String newpassword = in.next();
                    if (shop.changePassword(number, password, newpassword)) {
                        System.out.println("修改成功");
                        break;
                    }
                    System.out.println("修改失败");

                    break;
                case 6:
                    System.out.println("会员信息");
                    System.out.println(shop.getName()+","+shop.getAdr());
                    System.out.println(shop.getCardMap());
                    break;
                default:
                    Shop.storeObj(shop);//退出时存储
                    return;
            }

        }
    }
}

